package org.porridge.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.porridge.po.EntityPo;
import org.porridge.util.SVNManager;

/**
 * 
 * <p>Title: svnlogin   </p>
 * <p>Description: SVN登录</p>
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: svnkit</p>
 * <p>Create Date/Time: 2014年8月12日 下午8:26:12<p>
 * @author porridge
 * @version 1.0
 * @url www.prdblog.com
 *
 */
public class svnlogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		EntityPo po=new EntityPo();
		Field[] fies=po.getClass().getDeclaredFields();
		Object obj=null;
		String ret="";
		for(Field f:fies){
			f.setAccessible(true);
			obj=request.getParameter(f.getName());
			try {
			    if(obj!=null && obj.toString().length()>0){
					    f.set(po,obj.toString());
			    }else{
				    if("address".equals(f.getName()))//指定地址除外
				 	    f.set(po,"");
				    else
					    ret+="{\"key\":\""+f.getName()+"\",\"value\":\"不能为空\"},";
			     }
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		if(ret.length()==0){
			SVNManager svn=new SVNManager();
			boolean bl=svn.initialize(po);//初始化登录svn
			String temp=!bl?",errors:[{\"key\":\"登录错误信息\",\"value\":\"系统登录失败请检查输入后重试\"}]":"";
			out.print("{\"resoult\":\""+bl+"\""+temp+"}");
			if(bl)
				request.getSession().setAttribute("svn",svn);
		}else{
			out.print("{\"resoult\":\"false\",\"errors\":["+ret.substring(0,ret.length()-1)+"]}");
		}
	}

}
